class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        mid_list, now_note = [], head
        while now_note:
            mid_list.append(now_note)
            now_note = now_note.next
        mid_list.append(None)
        try:
            mid_list[-n-2].next = mid_list[-n]
        except:
            head = mid_list[-n+1]
        return head
